package com.tingyukeji.fastpay.login.ui.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.tingyukeji.fastpay.MainActivity;
import com.tingyukeji.fastpay.R;
import com.tingyukeji.fastpay.base.BaseActivity;
import com.tingyukeji.fastpay.databinding.LoginActivityBinding;
import com.tingyukeji.fastpay.entity.BaseReqEntity;
import com.tingyukeji.fastpay.entity.LoginPswEntity;
import com.tingyukeji.fastpay.entity.ReqCompletedLisener;
import com.tingyukeji.fastpay.utils.SharedPreferencesUtils;

/**
 * Created by Administrator on 2016/11/11.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private LoginActivityBinding binding;
    private SharedPreferencesUtils sp;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        super.setContentView();
        binding = DataBindingUtil.setContentView(this, R.layout.login_activity);
    }

    @Override
    protected void initView() {
        super.initView();
        sp = new SharedPreferencesUtils(this);
        binding.btLogin.setOnClickListener(this);
        binding.tvRegist.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_login:
                goLogin();
                break;
            case R.id.tv_regist:
                goRegist();
                break;
        }
    }

    private void goRegist() {
        goActivity(RegistActivity.class);
    }

    private void goLogin() {
        if (!TextUtils.isEmpty(binding.etPhone.getText().toString()) && !TextUtils.isEmpty(binding.etPsw.getText().toString())){
            BaseReqEntity loginPswEntity = new LoginPswEntity()
                    .putParam("mobilePhone",binding.etPhone.getText().toString())
                    .putParam("password",binding.etPsw.getText().toString())
                    .setReqType(BaseReqEntity.POST)
                    .setLisener(new ReqCompletedLisener<LoginPswEntity>() {
                        @Override
                        public void notifyReqCompleted(boolean success, LoginPswEntity data, String errMsg, boolean isCacheOpen) {
                            if (null != data){
                                Toast.makeText(LoginActivity.this,data.message,Toast.LENGTH_SHORT).show();
                                if (data.code == 1){
                                    sp.setParam("TOKEN", data.getData().token);
                                    goActivity(MainActivity.class);
                                    finish();
                                }
                            }else {
                                Toast.makeText(LoginActivity.this,"请求失败！",Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
            loginPswEntity.execute();
        }
    }
}
